package com.bbjob.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.annotations.ApiIgnore;

import com.bbjob.client.MsgBody;
import com.bbjob.client.SocketUser;
import com.bbjob.dto.Result;
import com.bbjob.entity.JwtUser;
import com.bbjob.model.SysDictionary;
import com.bbjob.model.SysOrganization;
import com.bbjob.model.User;
import com.bbjob.query.MachineInfo;
import com.bbjob.sec.SecContextUtil;
import com.bbjob.service.ISystemService;
import com.bbjob.service.impl.ServerSocketLine;
import com.bbjob.service.impl.UserServiceImpl;
import com.bbjob.toolutil.DataEntity;

@Api(tags = "systemApi", description = "系统管理")
@RestController
@RequestMapping(path = "system", produces = "application/json;charset=utf-8")
public class SystemController {
	
	@Resource
	private ISystemService systemService;
	
	@Resource
	private ServerSocketLine serverSocketLine;
	
	@Resource
	private UserServiceImpl userService;
	
	
	@ApiIgnore
	@ApiOperation(value="获取MAC地址", notes="局域网内根据ip获取机器MAC地址")
	@RequestMapping(value="getMac", method=RequestMethod.GET)
	public Result getMac(HttpServletRequest request) {
		
		return new Result(systemService.getMac(request));
	}

	@ApiOperation(value="获取组织列表", notes="根据pid获取下级组织")
	@ApiImplicitParam(name="organ", value="组织信息", required=true, dataType="SysOrganization")
	@RequestMapping(value="getOrganByPid", method=RequestMethod.POST)
	public Result getOrgans(@RequestBody SysOrganization organ) {
		
		return systemService.getOrgansByPid(organ);
	}	
	
	@ApiOperation(value="启动socket", notes="发布考试，启动socket")
	@RequestMapping(value="startServer", method=RequestMethod.GET)
	public void startSocketServer() {
		
		serverSocketLine.startServer();
	}	
	
	@ApiOperation(value="获取socket的IP列表", notes="")
	@RequestMapping(value="getSocketClient", method=RequestMethod.GET)
	public List<String> getClients() {
		
		Map<String, SocketUser> ips = serverSocketLine.getClients();
		
		return new ArrayList<String>(ips.keySet());
	}
	
	@ApiOperation(value="获取机器状态", notes="获取机器状态")
	@RequestMapping(value="getMachineInfo", method=RequestMethod.GET)
	public Result getMachineInfo(Integer examId) {
		
		List<MachineInfo> list = systemService.getMachineStatus(examId);
		
		return new Result(list); 
	}
	
	@ApiOperation(value="发送信息到考试端", notes="发送信息到考试端")
	@RequestMapping(value="sendMsg", method=RequestMethod.POST)
	public Result sendMsg(@RequestBody MsgBody msgBody) {
		
		return systemService.sendMsg(msgBody);
	}
	
	@RequestMapping(value = "getDic", method = RequestMethod.GET)
	@ResponseBody
	public Result getDic(SysDictionary dic) {
		return systemService.selectAllDics(dic);
	}
	
	
	@ApiOperation(value="发送信息到考试端", notes="发送信息到考试端")
	@RequestMapping(value="sendMsgs", method=RequestMethod.POST)
	public Result sendMsgs(@RequestBody List<MsgBody> msgsBody) {
		for (MsgBody item : msgsBody) {
			
			Result res = systemService.sendMsg(item);
			
			if (res.getCode() != 200) {
				return res;
			}
		}
		
		return new Result(200, "批量处理成功");
	}
	
	
	@ApiOperation(value="获取试卷类型", notes="获取试卷类型")
	@RequestMapping(value="getOperationAuth", method=RequestMethod.GET)
	public DataEntity getOperationAuth() {
		
		JwtUser user = SecContextUtil.getCurrentUser();
		
		// JwtUser user = (JwtUser)userService.loadUserByUsername(username);
		
		return systemService.getOperationAuth(user.getId());
	}
	
	@RequestMapping(value = "getDifficulty", method = RequestMethod.GET)
	@ResponseBody
	public DataEntity getDifficulty() {
		return systemService.getDifficulty();
	}
	
	/**
	 * 用户修改密码
	 * @param userid
	 * @return
	 */
	@RequestMapping(value = "changePassword", method = RequestMethod.GET)
	@ResponseBody
	public Result changePassword(@RequestParam(required = true) Integer userid,@RequestParam(required = true) String oldpassword,@RequestParam(required = true) String newpassword) {

		return systemService.changePassword(userid, oldpassword, newpassword);
	}
	
	@RequestMapping(value = "saveTeacher", method = RequestMethod.POST)
	@ResponseBody
	public Result saveTeacher(@RequestBody User user) {
		JwtUser jwtuser = SecContextUtil.getCurrentUser();
		
		return systemService.createUserByDirector(user, jwtuser.getId());
	}
	
	@RequestMapping(value = "editTeacher", method = RequestMethod.POST)
	@ResponseBody
	public Result editTeacher(@RequestBody User user) {
		return systemService.editTeacher(user);
	}
	
	@RequestMapping(value = "getTeacherList", method = RequestMethod.GET)
	@ResponseBody
	public DataEntity getTeacherList(User user, @RequestParam(required = false, defaultValue = "1") int page,
			@RequestParam(required = false, defaultValue = "10") int rows) {
		JwtUser jwtuser = SecContextUtil.getCurrentUser();
		return systemService.getTeacherList(user, page, rows,jwtuser.getId());
	}
	
	@RequestMapping(value = "deleteTeacher", method = RequestMethod.GET)
	@ResponseBody
	public Result deleteTeacher(Integer id) {
		JwtUser jwtuser = SecContextUtil.getCurrentUser();
		return systemService.deleteTeacher(id, jwtuser.getId());
	}
	
}
